package com.invengo.testrfid.mvp.presenter;

import android.app.Application;

import com.invengo.testrfid.app.dao.QueryManager;
import com.invengo.testrfid.app.dao.RfidCageBind;
import com.invengo.testrfid.app.dao.base.OperationDao;
import com.invengo.testrfid.app.entity.ReadTagEntity;
import com.invengo.testrfid.app.proxy.ProxyReadTag;
import com.invengo.testrfid.app.utils.ByteConvertUtils;
import com.invengo.testrfid.mvp.contract.CheckTagContract;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;


@ActivityScope
public class CheckTagPresenter extends BasePresenter<CheckTagContract.Model, CheckTagContract.View> {

    private QueryManager queryManager;


    @Inject
    public CheckTagPresenter(CheckTagContract.Model model, CheckTagContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);

        this.queryManager = QueryManager.getManager();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public void stopReadTag() {

        ProxyReadTag.getInstance().stopRead(new ProxyReadTag.StopReadCallback() {

            @Override
            public void onStopSuccess() {

            }

            @Override
            public void onStopFailed() {

                mRootView.showMessage("停止读取标签操作失败，请重试");
            }
        });
    }


    public void coonnect(){
        ProxyReadTag.getInstance().connectReader(new ProxyReadTag.ConnectReaderStatue() {
            @Override
            public void onConnectSuccess() {

            }

            @Override
            public void onConnectFailed() {

            }
        });
    }

    public void disConnect(){
        ProxyReadTag.getInstance().disConnectReader(new ProxyReadTag.DisConnectReaderStatue() {
            @Override
            public void onDisConnectSuccess() {

            }

            @Override
            public void onDisConnectFailed() {

            }
        });
    }

    /**
     * 标签单次读取
     */
    public void singleReadTag() {

        ProxyReadTag.getInstance().singleReadTagTid(new ProxyReadTag.ReadTagAllDataCallbac() {
            @Override
            public void onReadTagSuccess(ReadTagEntity readTagEntity) {
                handTagTwo(readTagEntity);
            }

            @Override
            public void onReadTagFailed(String msg) {

                mRootView.showMessage(msg);
            }
        }, new ProxyReadTag.StopReadCallback() {
            @Override
            public void onStopSuccess() {

            }

            @Override
            public void onStopFailed() {

//                mRootView.showMessage("停止读取标签失败");
            }
        });
    }

    private ReadTagEntity tagEntity;
    private RfidCageBind rfidCageBind;


    private void handTagTwo(ReadTagEntity readTagEntity){

        tagEntity = readTagEntity;
        rfidCageBind = queryManager.getRfidCageQuery().queryDataByTid(readTagEntity.getTid());
        mRootView.initShowTagView(readTagEntity,rfidCageBind== null?"":rfidCageBind.getCageId());
    }


    /**
     * 读取标签
     *
     * @param tid
     */
    private void handTag(ReadTagEntity tid) {

        tagEntity = tid;
        StringBuilder sb = new StringBuilder();
        sb.append("tid:");
        sb.append(tid.getTid() + "\n");
        sb.append("epc:");
        sb.append(tid.getEpc() + "\n");

        rfidCageBind = queryManager.getRfidCageQuery().queryDataByTid(tid.getTid());
        if (rfidCageBind != null) {
            sb.append("数据库数据：" + rfidCageBind.getCageId());

            if (rfidCageBind.getCageId().trim().equals(tid.getEpc().trim())) {
                mRootView.switchButton(false);
            } else {
                mRootView.switchButton(true);
            }
        } else {
            sb.append("未查询到绑定数据");
            mRootView.switchButton(false);
        }
        mRootView.initQueryData(sb.toString());
    }

    /**
     * 更新标签
     */
    public void updateTag() {

        ProxyReadTag.getInstance().writeDataToEpc(
                tagEntity.getTidData(), rfidCageBind.getCageId().trim().getBytes(),
                new ProxyReadTag.WriteTagEpcCallback() {
                    @Override
                    public void onWriteSuccess() {

                        mRootView.showMessage("初始化数据成功");
                        singleReadTag();
                    }

                    @Override
                    public void onWriteFailed() {

                        mRootView.showMessage("初始化数据失败");
                    }
                });


    }


}
